<?php
    include_once("../../includes/config.inc.php");
    check_login();
    $suserSession=get_suser();
    $customer_id=$suserSession['id'];
    $model=D('wx_send');
    $smarty->assign('cateName','群发素材');

    //根据id获取子图文的信息
    if($_REQUEST['a']=='getSubItem'){
        $res=$model->find((int)$_REQUEST['id']);
        echo json_encode($res);
        die;
    }

    //删除子图文
    if($_REQUEST['a']=='delDetail'){
        $model->del((int)$_REQUEST['id']);
        die('suc');
    }

    //敏感词
    $sense=array();
    $senseArr = D('wx_sense')->dataArr();
    foreach($senseArr as $k=>$v){
        if($v['word']) $sense[]=$v['word'];
    }
    $senseRow = $sense ? json_encode($sense) : '';
    $smarty->assign('senseRow',$senseRow);

    /**
     * 第一次添加图文
     */
    if($_REQUEST['a']=='add'){
        if(method_is('post')){
            $data=$_POST;
            $res=$model->insert(array(
                'customer_id'=>$customer_id,'info_title'=>$data['title'],
                'info_pic'=>$data['pic'],'msg_content'=>$data['msg_content'],
                'info_url'=>$data['url'],'order_num'=>$data['order_num']
            ));
            die('suc'.$res);
        }else{
            $smarty->setTpl('send/templates/multi_add.html')->display();die();
        }
    }

    /**
     * 图文修改
     * 一条信息对应info_commmon中一条数据
     * 一条信息可能对应keyword_list中多条数据
     */
    if($_REQUEST['a']=='edit'){
        if(method_is('post')){
            $data=$_POST;
            //第一次保存
            if($data['info_type_form']=='one'){//第一条图文的修改
                $model->updateById($data['id'],array(
                    'info_title'=>$data['title'],'info_pic'=>$data['pic'],
                    'msg_content'=>$data['msg_content'],'info_url'=>$data['url'],
                    'order_num'=>$data['order_num']
                ));
            }else{//非第一条图文
                if($data['child_id']){//修改
                    $model->updateById($data['child_id'],array(
                        'info_title'=>$data['title'],'info_pic'=>$data['pic'],
                        'msg_content'=>$data['msg_content'],'info_url'=>$data['url'],
                        'order_num'=>$data['order_num']
                    ));
                }else{//保存
                    $model->insert(array(
                        'customer_id'=>$customer_id,'pid'=>$data['id'],'info_title'=>$data['title'],
                        'info_pic'=>$data['pic'],'msg_content'=>$data['msg_content'],'info_url'=>$data['url'],
                        'order_num'=>$data['order_num']
                    ));
                }
            }
            die('suc');
        }else{
            $id=(int)$_GET['id'];
            if($id){
                $infoRow=$model->find($id);
                $infoRow['short_title']=cut_str(deletehtml($infoRow['info_title']),8);
                $smarty->assign('infoRow',$infoRow);
                //详情
                $infoAll=$model->where("pid='{$id}'")->order("order_num asc,id asc")->dataArr();
                foreach($infoAll as $k=>$v){
                    $infoAll[$k]['data_index']=$k+1;
                    $infoAll[$k]['short_title']=cut_str(deletehtml($v['info_title']),8);
                }
                $smarty->assign('infoAll',$infoAll);
            }
            $smarty->setTpl('send/templates/multi_edit.html')->display();die();
        }
    }

    //数据删除
    if($_REQUEST['a']=='del'){
        $res=$model->del((int)$_POST['id']);
        die('suc');
    }

    //数据列表
    $filter=array();
    $filter['where'] = "customer_id='{$customer_id}' and pid = 0 ";
    if($_GET['keyword']) $filter['where'].=" and info_title like '%".common_pg('keyword')."%'";
    $filter['order'] = "id desc";
    $data = $model->paginate($filter,'*',common_pg('p'),10);
    $listArr = $data['data'];
    foreach($listArr as $k=>$v){
        $dRow=$model->field("count(*) as count_num")->where("pid='".$v['id']."'")->dataRow();
        $listArr[$k]['item_num']=$dRow['count_num']+1;
    }

    $smarty->assign('list',$listArr);
    $smarty->assign('page',$model->pager($data['pager']));
    $smarty->assign('suserSession',$suserSession);

    $smarty->setTpl('send/templates/multi_index.html')->display();